from rest_framework import serializers
from goods.models import (
    HuaweiCategory,
    HuaweiSubCateGory,
    HuaweiGoods,
    HuaweiGoodContentImageList,
    HuaweiGoodsDetialImgList,
    HuaweiGoodDetialInfos,
)


class HuaweiCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = HuaweiCategory
        fields = "__all__"
        # read_only_fields = ["id","categoryName"]


class CategoruForSubcategory(serializers.ModelSerializer):
    class Meta:
        model = HuaweiCategory
        fields = ["id", "categoryName"]
        read_only_fields = ["id", "categoryName"]


class HuaweiSubCateGorySerializer(serializers.ModelSerializer):
    parentCategory = CategoruForSubcategory()

    class Meta:
        model = HuaweiSubCateGory
        fields = "__all__"
        read_only_fields = ["id", "subCategoryName", "parentCategory"]


# 定义一个序列化器来序列化goods里的外键数据，因为外键数据默认只返回id，但我还需要其他属性的值
class SubCategoryForGoods(serializers.ModelSerializer):
    class Meta:
        model = HuaweiSubCateGory
        fields = ["id", "subCategoryName", "parentCategory"]
        read_only_fields = ["id", "subCategoryName", "parentCategory"]


class HuaweiGoodsSerializer(serializers.ModelSerializer):
    goodCategory = SubCategoryForGoods()

    class Meta:
        model = HuaweiGoods
        fields = "__all__"
        read_only_fields = [
            "pk",
            "goodName",
            "goodCoverImg",
            "goodPrice",
            "goodDetailNmae",
            "goodCategory",
            "goodNums",
            "goodClickNums",
            "goodSoldNums",
            "goodShipFree",
            "goodFavNums",
            "good_is_new",
            "good_is_hot",
            "good_is_oncounting",
            "good_add_time",
        ]


# 更新点击量序列化器
class GoodClickNumSerializer(serializers.ModelSerializer):
    class Meta:
        model = HuaweiGoods
        fields = ["goodClickNums"]


class HuaweiGoodContentImageListSerializer(serializers.ModelSerializer):
    class Meta:
        model = HuaweiGoodContentImageList
        fields = "__all__"
        read_only_fields = ["pk", "images", "goods"]


class GoodsForDetailSImages(serializers.ModelSerializer):
    class Meta:
        model = HuaweiGoods
        fields = ["pk","goodName", "goodPrice"]
        read_only_fields = ["pk","goodName", "goodPrice"]


class HuaweiGoodsDetialImgListSerializer(serializers.ModelSerializer):
    goods = GoodsForDetailSImages()

    class Meta:
        model = HuaweiGoodsDetialImgList
        fields = "__all__"
        read_only_fields = ["pk", "images", "imagessmall"]


class HuaweiGoodDetialInfosSerializer(serializers.ModelSerializer):
    class Meta:
        model = HuaweiGoodDetialInfos
        fields = "__all__"
        read_only_fields = ["pk", "detailName", "detialValue", "goods"]

